package com.five.matebook.utils;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

public class StringTreeParser {
    private int mLevelNum;
    public StringTreeParser(Context context, int resId) {
        this(context, resId, 2);
    }

    public StringTreeParser(Context context, int resId, int levelNum) {
        this(context, null, resId, levelNum);
    }

    private StringItem mRoot = new StringItem("#root");
    public StringTreeParser(Context context, List<String> perfArr, int resId, int levelNum) {
        this.mLevelNum = levelNum;
        if (levelNum <= 0) {
            levelNum = 2;
        }

        String[] arr = null;
        if (perfArr != null && perfArr.size() > 0) {
            arr = perfArr.toArray(new String[0]);
        } else {
            arr = context.getResources().getStringArray(resId);
        }
        for(String s : arr) {
            // 去除空行
            if ("".equals(s.trim())) {
                continue;
            }
            StringItem p = this.mRoot;
            String[] temp = s.trim().split(" ");
            for (int i=0; i<temp.length; i++) {
                if (i + 1 == levelNum) {
                    // 后面的都放到最后一层里面
                    for (int j=i; j<temp.length; j++) {
                        p.addSubItem(new StringItem(temp[j]));
                    }
                    break;
                } else {
                    StringItem sub = new StringItem(temp[i]);
                    p.addSubItem(sub);
                    p = sub;
                }
            }
        }
    }

    public synchronized List<StringItem> getArray(StringItem parent) {
        return getArray(parent, true);
    }

    public synchronized List<StringItem> getArray(StringItem parent, boolean autoAddBlankAtHead) {
        if (parent != null) {
            List<StringItem> ret = new ArrayList<>();
            if (autoAddBlankAtHead) {
                ret.add(new StringItem(""));
            }
            ret.addAll(parent.subs);
            return ret;
        }
        return null;
    }

    public synchronized List<StringItem> getArray(int level) {
        return getArray(level, true);
    }
    public synchronized List<StringItem> getArray(int level, boolean autoAddBlankAtHead) {
        if (level < 0 || level > this.mLevelNum) {
            return null;
        }
        List<StringItem> ret = new ArrayList<>();
        if (autoAddBlankAtHead) {
            ret.add(new StringItem(""));
        }
        mRoot.trasverByLevel(level, ret);
        return ret;
    }

    public static String[] toStringArray(List<StringItem> list) {
        return toStringArray(list, false);
    }

    public static String[] toStringArray(List<StringItem> list, boolean addEmptyStringAtHead) {
        if (list != null) {
            if (addEmptyStringAtHead) {
                String[] ret = new String[list.size() + 1];
                ret[0] = "";
                for (int i = 0; i < list.size(); i++) {
                    ret[i + 1] = list.get(i).toString();
                }
                return ret;
            } else {
                String[] ret = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    ret[i] = list.get(i).toString();
                }
                return ret;
            }
        }
        return null;
    }

    public class StringItem {
        private String value;
        private int mLevel = 0;
        private List<StringItem> subs = new ArrayList<>();
        StringItem(String value) {
            this.value = value;
        }

        public List<StringItem> getSubList() {
            return subs;
        }

        void addSubItem(StringItem sub) {
            if (sub == null) {
                return;
            }
            this.subs.add(sub);
            sub.mLevel = this.mLevel + 1;
        }

        void trasverByLevel(int level, List<StringItem> ret) {
            if (this.mLevel == level) {
                ret.add(this);
            } else if (this.mLevel < level) {
                for (StringItem sub : this.subs) {
                    sub.trasverByLevel(level, ret);
                }
            }
        }

        @Override
        public String toString() {
            return this.value;
        }
    }
}


